import os from '@/assets/api/os'
import axios from 'axios';
import Vue from 'vue'

const instance = axios.create({
    timeout: 20000,
    withCredentials: true,
    headers: {
        'Content-type': 'application/json;charset=utf-8'
    }
});

Vue.prototype.$axios =  instance;

(function(ctx){
    ctx.needLoading = true;

    instance.interceptors.request.use(config => {
        if (ctx.needLoading) {
            this.$Indicator.open({
                text: '加载中...',
                spinnerType: 'fading-circle'
            });
        }

        // 在发送请求之前做些什么
        let token = localStorage.getItem("token");
        if (token) {
            config.headers.Authorization = localStorage.getItem("token");
            config.headers.platform = 'lxk';
            config.withCredentials = true;
        }
        return config;
    }, error => {
        this.$Indicator.close();
        // 对请求错误做些什么
        this.$Toast("请求出错")
        return Promise.reject(error);
    });

    // 添加响应拦截器
    instance.interceptors.response.use(response => {

        if (response.headers && (response.headers.Authorization || response.headers.authorization)) {
            localStorage.setItem('token', response.headers.Authorization || response.headers.authorization)
        }
        var headerToken = response.headers.Authorization || response.headers.authorization;

        // localStorage.setItem('forceLogin', 'false');

        // 对响应数据做点什么
        if (response.data.code != 0) {
            if (response.data.code == 220001 ||
                response.data.code == 220005 ||
                response.data.code == 220008 ||
                response.data.code == 220010 ||
                response.data.code == 220010 ||
                response.data.code == 200001) {
                this.$Toast(response.data.msg);
                ctx.$nuxt.$router.push({ name: "login" })
            } else if (response.data.code == 220013) {
                ctx.$nuxt.$confirm('账号登出提示', response.data.msg, '忽略', '重新登录')
                    .then(() => {
                        localStorage.removeItem("token");
                        ctx.$nuxt.$router.push({ name: "login" })
                    })
                    .catch(() => {
                        localStorage.removeItem("token");
                        ctx.$nuxt.$router.push({ name: "login" })
                    })
            } else if (response.data.code == 220026 || response.data.code == 220011) {
                ctx.$nuxt.$confirm('友情提示', response.data.msg, '取消', '拨打电话')
                    .then(() => {
                        const tel = response.data.data.service_tel || '400-101-5559';
                        if (os.isIOS()) {
                            ctx.location.href = `tel://${tel}`;
                        } else if (os.isAndroidLxkApp()) {
                            ctx.control.call_phone(tel);
                        } else {
                            ctx.location.href = `tel://${tel}`;
                        }
                    }).catch(() => {})
            } else {
                if (ctx.$nuxt.$route.name != 'signinIndex') {
                    this.$Toast(response.data.msg);
                }
            }
        }
        if (ctx.needLoading) {
            this.$Indicator.close();
        }
        return response;
    }, error => {
        this.$Indicator.close();
        // 对响应错误做点什么
        this.$Toast("网络错误");
        return Promise.reject(error);
    });

    
}).call(
    new Vue,
    typeof window=='Object'?window:global
)

